package com.yunhe.rentcars.pro.controller.car;

import com.yunhe.rentcars.commons.dto.JSONResponse;
import com.yunhe.rentcars.commons.dto.Pageable;
import com.yunhe.rentcars.pro.entity.rent.Cars;
import com.yunhe.rentcars.pro.service.car.CarsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author maliang
 * @version V1.0
 * @date 2017/11/28 0028 08:58
 * @Description: 车辆信息控制器
 */
@Controller
@RequestMapping("/cars")
public class CarsController {
    @Autowired
    private CarsService carsService;

    @RequestMapping
    public String list(){
        return "car/cars/list";
    }

    @RequestMapping("/add")
    public String add() {
        return "car/cars/add";
    }

    @RequestMapping("/edit")
    public String edit(Long carid, HttpServletRequest request) {
        Cars cars = carsService.findOne(carid,null);
        request.setAttribute("cars",cars);
        return "car/cars/edit";
    }

    @RequestMapping("/save")
    @ResponseBody
    public JSONResponse save(Cars cars){
        carsService.save(cars);
        return  JSONResponse.success("创建成功！");
    }

    @RequestMapping("/update")
    @ResponseBody
    public JSONResponse update(Cars cars){
        carsService.update(cars);
        return JSONResponse.success("修改成功！");
    }

    @RequestMapping("/delBatch")
    @ResponseBody
    public JSONResponse delBatch(String ids){
        carsService.delBatch(ids);
        return JSONResponse.success("删除成功！");
    }


    @RequestMapping("/listData")
    @ResponseBody
    public Pageable<Cars> search(Cars cars, Integer offset, Integer limit){
        return carsService.findList(cars,offset,limit);
    }

    @RequestMapping("/carState")
    @ResponseBody
    public JSONResponse carState(Cars cars){
        carsService.carState(cars);
        return JSONResponse.success("修改成功！");
    }

    @RequestMapping("/carCodeCheck")
    @ResponseBody
    public JSONResponse carCodeCheck(String carcode){
        Cars uniqe = carsService.findOne(null, carcode);
        boolean data = false;
        if(null != uniqe) {
            data = true;
        }
        return JSONResponse.success("查找成功！",data);
    }
}
